package com.bs.blogspace.mapper;

import com.bs.blogspace.pojo.SpaceArticle;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface SpaceArticleMapper {
    /* 添加文章  */
    @Insert("insert into space_article(space_id,phone,content,imgs,create_time,update_time) values (#{spaceId},#{phone},#{content},#{imgs},now(),now())")
    int addArticle(SpaceArticle spaceArticle);

    /* 删除文章 */
    @Delete("delete from space_article where essay_id=#{essayId}")
    int deleteArticle(String essayId);

    /* 根据主键查询文章 */
    @Select("SELECT * from space_article where essay_id=#{essayId}")
    SpaceArticle showByEssayId(String essayId);

    /*根据spaceID查询空间的文章*/
    @Select("select a.*,b.user_name,b.avatar from\n" +
            "(select essay_id,phone,imgs,content,create_time from space_article where space_id = #{spaceId}) as a\n" +
            "join\n" +
            "(select phone,user_name,avatar from user where phone in (select phone from space_article where space_id = #{spaceId})) as b\n" +
            "on a.phone = b.phone; ")
    List<SpaceArticle> getEssay(String spaceId);
}
